package com.example.simple_android_examples.activity;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.example.simple_android.activity.BaseActivity;
import com.example.simple_android.annotation.InitView;
import com.example.simple_android.annotation.Layout;
import com.example.simple_android.annotation.Receiver;
import com.example.simple_android_examples.R;
import com.example.simple_android_examples.constant.Constant;

@SuppressLint("NonConstantResourceId")
@Layout(layout = R.layout.activity_main)
public class MainActivity extends BaseActivity {

    @InitView(viewId = R.id.tips)
    private TextView tips;

    // 设为跟随外部类启动而注册
    @Receiver(actions = Constant.InnerReceiverConstant.ACTION, whenToStart = com.example.simple_android.constant.Constant.ReceiverConstant.FOLLOW_EXTERNAL_CLASS)
    public static class InnerBroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (Constant.InnerReceiverConstant.ACTION.equals(intent.getAction())) {
                Bundle extras = intent.getExtras();
                String string = extras.getString(Constant.InnerReceiverConstant.FLAG);
                System.err.println("成员内部类广播接收者，收到消息：".concat(string));
            }
        }
    }

    @Receiver(actions = Constant.AnonymousInnerClassConstant.ACTION)
    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (Constant.AnonymousInnerClassConstant.ACTION.equals(intent.getAction())) {
                Bundle extras = intent.getExtras();
                String string = extras.getString(Constant.AnonymousInnerClassConstant.FLAG);
                System.err.println("匿名内部类接收到广播消息：".concat(string));
            }
        }
    };

    @Override
    protected void initClick() {
        tips.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Constant.StandardReceiverConstant.ACTION);
                Bundle bundle = new Bundle();
                bundle.putString(Constant.StandardReceiverConstant.FLAG, "向标准广播接收者发送消息");
                intent.putExtras(bundle);
                sendBroadcast(intent);
                intent = new Intent(Constant.NoStandardReceiverConstant.ACTION);
                bundle = new Bundle();
                bundle.putString(Constant.NoStandardReceiverConstant.FLAG, "向非标准广播接收者发送消息");
                intent.putExtras(bundle);
                sendBroadcast(intent);
                intent = new Intent(Constant.InnerReceiverConstant.ACTION);
                bundle = new Bundle();
                bundle.putString(Constant.InnerReceiverConstant.FLAG, "向成员内部类发送的消息");
                intent.putExtras(bundle);
                sendBroadcast(intent);
                intent = new Intent(Constant.AnonymousInnerClassConstant.ACTION);
                bundle = new Bundle();
                bundle.putString(Constant.AnonymousInnerClassConstant.FLAG, "向匿名内部类发送的消息");
                intent.putExtras(bundle);
                sendBroadcast(intent);
                intent = new Intent(Constant.ObjectEnhancedReceptionConstant.ACTION);
                bundle = new Bundle();
                bundle.putString(Constant.ObjectEnhancedReceptionConstant.FLAG, "向增强对象接收者，发送的广播消息");
                intent.putExtras(bundle);
                sendBroadcast(intent);
            }
        });
    }

    @Receiver(actions = Constant.ObjectEnhancedReceptionConstant.ACTION)
    private void objectEnhancedReception(Context context, Intent intent) {
        if (Constant.ObjectEnhancedReceptionConstant.ACTION.equals(intent.getAction())) {
            Bundle extras = intent.getExtras();
            String string = extras.getString(Constant.ObjectEnhancedReceptionConstant.FLAG);
            System.err.println("对象增强接收者收到消息：".concat(string));
        }
    }
}